package com.learn.thread;

import cn.hutool.http.HttpUtil;

import java.io.File;
import java.util.concurrent.*;

/**
 * 实现多线程方式三：实现Callable
 * 1. 实现Callable
 * 2. 创建Callable对象
 * 3. 创建FutureTask
 * 4. 创建Thread
 * 5. 启动线程
 * 6. 获取结果
 * 7. 关闭线程池
 * 8. 获取结果
 */
public class TestCallable implements Callable<Boolean> {
    private String url;
    private String targetName;

    public TestCallable(String url, String targetName) {
        this.url = url;
        this.targetName = targetName;
    }

    @Override
    public Boolean call() throws Exception {
        HttpUtil.downloadFile(url, new File(targetName));
        System.out.println("当前下载的文件名称为" + targetName);
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(new TestCallable("https://oss.javaguide.cn/github/javaguide/books/image-20220424103035793.png", "src/main/resources/static/t7.png"));
        executorService.submit(new TestCallable("https://oss.javaguide.cn/github/javaguide/books/image-20220424101217849.png", "src/main/resources/static/t8.png"));
        executorService.submit(new TestCallable("https://oss.javaguide.cn/github/javaguide/books/image-20220424103124893.png", "src/main/resources/static/t9.png"));
        executorService.shutdown();
    }
}


